﻿using ShopFine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShopFine
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    int productID = Convert.ToInt32(Request.QueryString["id"]);
                    LoadProduct(productID);
                    LoadFeaturedList();
                    LoadSpecialList();
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }

        private void LoadSpecialList()
        {
            var featureds = new ProductDAO().GetFeaturedList().Take(4).ToList();
            UcSpecialVertical.FeaturedList = featureds;
            UcSpecialVertical.LoadFeaturedList();
        }

        private void LoadFeaturedList()
        {
            var featureds = new ProductDAO().GetFeaturedList().Take(4).ToList();
            UcFeaturedVertical.FeaturedList = featureds;
            UcFeaturedVertical.LoadFeaturedList();
        }

        private void LoadProduct(int productID)
        {
            var product = new ProductDAO().GetProductByID(productID);
            if (product != null)
            {
                ImgKey.ImageUrl = product.ImageUrl;
                fancyboxUrl.HRef = product.ImageUrl;
                productTitle.Text = string.Format("<a href='CategoryList.aspx?id={0}'>{1}</a> -> <a href='SubCategory.aspx?id={2}'>{3}</a> -> {4}",
                    product.SubCategory.CategoryID, product.SubCategory.Category.Name, product.SubCategoryID, product.SubCategory.Name, product.Name);// product.Name;
                productPrice.InnerText = product.Price.ToString("$#,##0");
                ltrProductCode.Text = product.ProductCode;
                lblDescription.Text = product.Description ?? string.Empty;
                ltrManfactuer.Text = product.Supplier == null ? string.Empty : product.Supplier.Name;
                var imageUrls = new ImageUrlDAO().GetImgUrlsByProductID(productID);

                rptImageUrls.DataSource = imageUrls;
                rptImageUrls.DataBind();
            }
        }
    }
}